package com.fanjun.androiddemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fanjun.androiddemo.R;

import java.util.List;
import java.util.Map;

public class BookListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<Map<String, Object>> list;
    private Context context;

    public BookListAdapter(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = mInflater.inflate(R.layout.activity_book_list, null);
            holder.name = view.findViewById(android.R.id.text1);
            holder.pic = view.findViewById(R.id.iv);
            Glide.with(context).load(list.get(i).get("pic")).into(holder.pic);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.name.setText((String) list.get(i).get("name"));
        return view;
    }

    public final class ViewHolder {

        private TextView name;
        private ImageView pic;
    }
}
